<?php
namespace app\admin\controller;

use app\admin\middleware\CheckAdmin;
use app\MyException;
use app\service\admin\RecordService;
use think\facade\Db;
use think\annotation\route\Group;
use think\annotation\Route;
use think\annotation\route\Middleware;
/**
 * 项目列表
 * Class Record
 * @package app\admin\controller
 * @Group("record")
 * @middleware({CheckAdmin::class})
 */
class Record extends Base
{
    /**
     * @Route("index", method="POST")
     *
     * @middleware({CheckAdmin::class})
     * @return \json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $page = input('page', 1, 'intval');
        $pagesize = input('psize', 20, 'intval');
        $name = input('name', 20, 'intval');
        $lists = Db::name('project_records')
            ->order(['id' => 'desc'])
            ->page($page, $pagesize)->select()->all();
        foreach ($lists as $k => $v) {
            $project_cate = Db::name('project_cate')->where('id',$v['project_cate'])->find();
            $plan_cate = Db::name('plan_cate')->where('id',$v['plan_cate'])->find();
            $support_area = Db::name('support_area')->where('id',$v['support_area'])->find();
            $project_cycle = Db::name('project_cycle')->where('id',$v['project_cycle'])->find();
            $level = Db::name('level')->where('id',$v['level'])->find();
            $department = Db::name('department')->where('id',$v['department'])->find();
            $budget = Db::name('budget')->where('project_records_id', $v['id'])->select();
            $reimbursement = Db::name('reimbursement')->where('project_records_id',$v['id'])->select();
            $lists[$k]['reimbursement'] = $reimbursement;
            $lists[$k]['project_cate'] = $project_cate ? $project_cate['name'] : "";
            $lists[$k]['plan_cate'] = $plan_cate ? $plan_cate['name'] : "";
            $lists[$k]['support_area'] = $support_area ? $support_area['name'] : "";
            $lists[$k]['project_cycle'] = $project_cycle ? $project_cycle['name'] : "";
            $lists[$k]['level'] = $level ? $level['name'] : "";
            $lists[$k]['department'] = $department ? $department['name'] : "";
            $lists[$k]['budget'] = $budget;
        }
        $result['total'] = Db::name('project_records')->count();
        $result['data'] = $lists;
        return json_ok($result);
    }

    /**
     * @Route("save", method="POST")
     *@middleware({CheckAdmin::class})
     */
    public function save()
    {
        $data = [
            'plan_cate' => input('plan_cate', '', 'trim'),
            'name' => input('name', '', 'trim'),
            'support_area' => input('support_area', '', 'trim'),
            'project_cycle' => input('project_cycle', '', 'trim'),
            'project_cate' => input('project_cate', '', 'trim'),
            'project_code' => input('project_code', '', 'trim'),
            'project_funds' => input('project_funds', '', 'trim'),
            'source' => input('source', '', 'trim'),
            'level' => input('level', '', 'trim'),
            'department' => input('department', '', 'trim'),
            'charge_person' => input('charge_person', '', 'trim'),
            'charge_phone' => input('charge_phone', '', 'trim'),
            'link_person' => input('link_person', '', 'trim'),
            'link_phone' => input('link_phone', '', 'trim'),
            'file_id' => input('file_id', '', 'trim'),
            'type' => input('type', '', 'trim'),
            'total_person' => input('total_person', '', 'trim'),
            'senior_nums' => input('senior_nums', '', 'trim'),
            'middle_nums' => input('middle_nums', '', 'trim'),
            'primary_nums' => input('primary_nums', '', 'trim'),
            'talents_nums' => input('talents_nums', '', 'trim'),
            'postdoctor_nums' => input('postdoctor_nums', '', 'trim'),
            'doctor' => input('doctor', '', 'trim'),
            'cource' => input('cource', '', 'trim'),
            'member_list' => input('member_list', '', 'trim'),
        ];
        $res = RecordService::save($data);
        //处理项目成员
        if (empty($data['member_list'])) {
            throw new MyException(10012, 'member_list不能为空');
        }
        $member_list = $data['member_list'];
        foreach ($member_list as &$v){
            $v['record_id'] = $res;
        }
        Db::name('project_record_member')->insertAll($member_list);
        if(!$res){
            throw new MyException(10012);
        }
        return json_ok([], 10000, '操作成功');

    }

    /**
     * @Route("saveBudget", method="POST")
     *
     * @middleware({CheckAdmin::class})
     */
    public function saveBudget()
    {
        $dateTime = new \DateTime(input('year', '', 'trim'));
        $year = $dateTime->format('Y');
        $result = Db::name('budget')->where('year', $year)->find();
        if ($result) {
            return json_error(10011, '该年度数据已存在');
        }
        $data = [
            'project_records_id' => input('project_records_id', '', 'trim'),
            'project_funds' => input('project_funds', '', 'trim'),
            'receive_funds' => input('receive_funds', '', 'trim'),
            'pay_funds' => input('pay_funds', '', 'trim'),
            'remain_funds' => input('remain_funds', '', 'trim'),
            'year' => $year,
            'device_funds' => input('device_funds', '', 'trim'),
            'device_funds_content' => input('device_funds_content', '', 'trim'),
            'buy_device_funds' => input('buy_device_funds', '', 'trim'),
            'buy_device_funds_content' => input('buy_device_funds_content', '', 'trim'),
            'make_device_funds' => input('make_device_funds', '', 'trim'),
            'make_device_funds_content' => input('make_device_funds_content', '', 'trim'),
            'change_funds' => input('change_funds', '', 'trim'),
            'change_funds_content' => input('change_funds_content', '', 'trim'),
            'material_funds' => input('material_funds', '', 'trim'),
            'material_funds_content' => input('material_funds_content', '', 'trim'),
            'test_funds' => input('test_funds', '', 'trim'),
            'test_funds_content' => input('test_funds_content', '', 'trim'),
            'energy_funds' => input('energy_funds', '', 'trim'),
            'energy_funds_content' => input('energy_funds_content', '', 'trim'),
            'travel_funds' => input('travel_funds', '', 'trim'),
            'travel_funds_content' => input('travel_funds_content', '', 'trim'),
            'meeting_funds' => input('meeting_funds', '', 'trim'),
            'meeting_funds_content' => input('meeting_funds_content', '', 'trim'),
            'chat_funds' => input('chat_funds', '', 'trim'),
            'chat_funds_content' => input('chat_funds_content', '', 'trim'),
            'news_funds' => input('news_funds', '', 'trim'),
            'news_funds_content' => input('news_funds_content', '', 'trim'),
            'service_funds' => input('service_funds', '', 'trim'),
            'service_funds_content' => input('service_funds_content', '', 'trim'),
            'consulting_funds' => input('consulting_funds', '', 'trim'),
            'consulting_funds_content' => input('consulting_funds_content', '', 'trim'),
            'performance_pay' => input('performance_pay', '', 'trim'),
            'performance_pay_content' => input('performance_pay_content', '', 'trim'),
            'other_pay' => input('other_pay', '', 'trim'),
            'other_pay_content' => input('other_pay_content', '', 'trim'),
            'total_funds' => input('total_funds', '', 'trim'),
            'remark' => input('remark', '', 'trim'),
        ];
        Db::name('budget')->insert($data);
        return json_ok([], 10000, '操作成功');
    }

    /**
     * @Route("saveReimbursement", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function saveReimbursement()
    {
        $dateTime = new \DateTime(input('year', '', 'trim'));
        $year = $dateTime->format('Y');
        $result = Db::name('reimbursement')->where('year', $year)->find();
        if ($result) {
            return json_error(10011, '该年度数据已存在');
        }
        $data = [
            'project_records_id' => input('project_records_id', '', 'trim'),
            'project_funds' => input('project_funds', '', 'trim'),
            'receive_funds' => input('receive_funds', '', 'trim'),
            'pay_funds' => input('pay_funds', '', 'trim'),
            'remain_funds' => input('remain_funds', '', 'trim'),
            'year' => $year,
            'device_funds' => input('device_funds', '', 'trim'),
            'device_funds_content' => input('device_funds_content', '', 'trim'),
            'buy_device_funds' => input('buy_device_funds', '', 'trim'),
            'buy_device_funds_content' => input('buy_device_funds_content', '', 'trim'),
            'buy_device_funds_files' => input('buy_device_funds_files', '', 'trim'),
            'make_device_funds' => input('make_device_funds', '', 'trim'),
            'make_device_funds_content' => input('make_device_funds_content', '', 'trim'),
            'make_device_funds_files' => input('make_device_funds_files', '', 'trim'),
            'change_funds' => input('change_funds', '', 'trim'),
            'change_funds_content' => input('change_funds_content', '', 'trim'),
            'change_funds_files' => input('change_funds_files', '', 'trim'),
            'material_funds' => input('material_funds', '', 'trim'),
            'material_funds_content' => input('material_funds_content', '', 'trim'),
            'material_funds_files' => input('material_funds_files', '', 'trim'),
            'test_funds' => input('test_funds', '', 'trim'),
            'test_funds_content' => input('test_funds_content', '', 'trim'),
            'test_funds_files' => input('test_funds_files', '', 'trim'),
            'energy_funds' => input('energy_funds', '', 'trim'),
            'energy_funds_content' => input('energy_funds_content', '', 'trim'),
            'energy_funds_files' => input('energy_funds_files', '', 'trim'),
            'travel_funds' => input('travel_funds', '', 'trim'),
            'travel_funds_content' => input('travel_funds_content', '', 'trim'),
            'travel_funds_files' => input('travel_funds_files', '', 'trim'),
            'meeting_funds' => input('meeting_funds', '', 'trim'),
            'meeting_funds_content' => input('meeting_funds_content', '', 'trim'),
            'meeting_funds_files' => input('meeting_funds_files', '', 'trim'),
            'chat_funds' => input('chat_funds', '', 'trim'),
            'chat_funds_content' => input('chat_funds_content', '', 'trim'),
            'chat_funds_files' => input('chat_funds_files', '', 'trim'),
            'news_funds' => input('news_funds', '', 'trim'),
            'news_funds_content' => input('news_funds_content', '', 'trim'),
            'news_funds_files' => input('news_funds_files', '', 'trim'),
            'service_funds' => input('service_funds', '', 'trim'),
            'service_funds_content' => input('service_funds_content', '', 'trim'),
            'service_funds_files' => input('service_funds_files', '', 'trim'),
            'consulting_funds' => input('consulting_funds', '', 'trim'),
            'consulting_funds_content' => input('consulting_funds_content', '', 'trim'),
            'consulting_funds_files' => input('consulting_funds_files', '', 'trim'),
            'performance_pay' => input('performance_pay', '', 'trim'),
            'performance_pay_content' => input('performance_pay_content', '', 'trim'),
            'performance_pay_files' => input('performance_pay_files', '', 'trim'),
            'other_pay' => input('other_pay', '', 'trim'),
            'other_pay_content' => input('other_pay_content', '', 'trim'),
            'other_pay_files' => input('other_pay_files', '', 'trim'),
            'total_funds' => input('total_funds', '', 'trim'),
            'remark' => input('remark', '', 'trim'),
        ];
        Db::name('reimbursement')->insert($data);
        return json_ok([], 10000, '操作成功');
    }


    /**
     * @Route("getPlanCate", method="POST")
     *@middleware({CheckAdmin::class})
     */
    public function getPlanCate()
    {
        $res = Db::name('plan_cate')->select();
        return json_ok($res);
    }

    /**
     * @Route("getSupportArea", method="POST")
     *@middleware({CheckAdmin::class})
     */
    public function getSupportArea()
    {
        $res = Db::name('support_area')->select();
        return json_ok($res);
    }

    /**
     * @Route("getProjectCycle", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function getProjectCycle()
    {
        $res = Db::name('project_cycle')->select();
        return json_ok($res);
    }

    /**
     * @Route("getProjectCate", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function getProjectCate()
    {
        $res = Db::name('project_cate')->select();
        return json_ok($res);
    }

    /**
     * @Route("getLevel", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function getLevel()
    {
        $res = Db::name('level')->select();
        return json_ok($res);
    }

    /**
     * @Route("getDepartment", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function getDepartment()
    {
        $res = Db::name('department')->select();
        return json_ok($res);
    }

    /**
     * @Route("getProfession", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function getProfession()
    {
        $res = Db::name('profession')->select();
        return json_ok($res);
    }

    /**
     * @Route("getYearByBudget", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function getYearByBudget()
    {
        $id = input('id', '', 'trim');
        $res = Db::name('project_budget')->field('id,year,status')->where('project_records_id', $id)->select();
        return json_ok($res);
    }

    /**
     * @Route("getYearByReimbursement", method="POST")
     * @middleware({CheckAdmin::class})
     *
     */
    public function getYearByReimbursement()
    {
        $id = input('id', '', 'trim');
        $res = Db::name('reimbursement')->field('id,year,status')->where('project_records_id', $id)->select();
        return json_ok($res);
    }

    /**
     * @Route("getDetailByBudget", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function getDetailByBudget()
    {
       $id = input('id', '', 'trim');
       $res = Db::name('budget')->where('id', $id)->find();
       return json_ok($res);
    }

    /**
     * @Route("getDetailByReimbursement", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function getDetailByReimbursement()
    {
        $id = input('id', '', 'trim');
        $res = Db::name('reimbursement')->where('id', $id)->find();
        return json_ok($res);
    }

}